home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Docs / Regular_Expressions_1.txt < prev    next >
Text File  |  1998-06-24  |  9KB  |  207 lines

  1. Newsgroups: comp.lang.python
  2. From: borbor@divsun.unige.ch (Boris Borcic)
  3. Subject: Re: Regular expressions, pattern matching
  4. Date: Mon, 27 Nov 1995 08:50:58 GMT
  5.  
  6. In article <49aml2$n82@mrnews.mro.dec.com>, Peter.Mayne@cao.mts.dec.com
  7. (Peter Mayne) wrote:
  8.  
  9. > Please don't point me to any Emacs doc. I don't use Emacs, and I don't
  10. want to 
  11. > use Emacs, I just want to know how regular expressions and pattern matching 
  12. > work in Python.
  13.  
  14. Then you have a problem. Note that you don't need to use emacs in order
  15. to use a part of its documentation. If you insist, you might try to use
  16. the MOO doc on regexps that uses the same package (it's really the
  17. same doc with the backslash escape changed to percent).
  18.  
  19. Just in case you change your mind, I append the essential part
  20. of the emacs doc :-)
  21.  
  22. Regards,
  23.  
  24. Boris Borcic
  25. ==
  26.  
  27. Syntax of Regular Expressions
  28. =============================
  29.  
  30.    Regular expressions have a syntax in which a few characters are
  31. special constructs and the rest are "ordinary".  An ordinary character
  32. is a simple regular expression which matches that character and nothing
  33. else.  The special characters are `$', `^', `.', `*', `+', `?', `[',
  34. `]' and `\'; no new special characters will be defined.  Any other
  35. character appearing in a regular expression is ordinary, unless a `\'
  36. precedes it.
  37.  
  38.    For example, `f' is not a special character, so it is ordinary, and
  39. therefore `f' is a regular expression that matches the string `f' and
  40. no other string.  (It does not match the string `ff'.)  Likewise, `o'
  41. is a regular expression that matches only `o'.
  42.  
  43.    Any two regular expressions A and B can be concatenated.  The result
  44. is a regular expression which matches a string if A matches some amount
  45. of the beginning of that string and B matches the rest of the string.
  46.  
  47.    As a simple example, you can concatenate the regular expressions `f'
  48. and `o' to get the regular expression `fo', which matches only the
  49. string `fo'.  To do something nontrivial, you need to use one of the
  50. following special characters:
  51.  
  52. `. (Period)'
  53.      is a special character that matches any single character except a
  54.      newline.  Using concatenation, you can make regular expressions
  55.      like `a.b', which matches any three-character string which begins
  56.      with `a' and ends with `b'.
  57.  
  58. `*'
  59.      is not a construct by itself; it is a suffix, which means the
  60.      preceding regular expression is to be repeated as many times as
  61.      possible.  In `fo*', the `*' applies to the `o', so `fo*' matches
  62.      one `f' followed by any number of `o's.  The case of zero `o's is
  63.      allowed: `fo*' does match `f'.
  64.  
  65.      `*' always applies to the smallest possible preceding expression.
  66.      Thus, `fo*' has a repeating `o', not a repeating `fo'.
  67.  
  68.      The matcher processes a `*' construct by immediately matching as
  69.      many repetitions as it can find.  Then it continues with the rest
  70.      of the pattern.  If that fails, backtracking occurs, discarding
  71.      some of the matches of the `*'-modified construct in case that
  72.      makes it possible to match the rest of the pattern.  For example,
  73.      matching `ca*ar' against the string `caaar', the `a*' first tries
  74.      to match all three `a's; but the rest of the pattern is `ar' and
  75.      there is only `r' left to match, so this try fails.  The next
  76.      alternative is for `a*' to match only two `a's.  With this choice,
  77.      the rest of the regexp matches successfully.
  78.  
  79. `+'
  80.      is a suffix character similar to `*' except that it requires that
  81.      the preceding expression be matched at least once.  For example,
  82.      `ca+r' will match the strings `car' and `caaaar' but not the
  83.      string `cr', whereas `ca*r' would match all three strings.
  84.  
  85. `?'
  86.      is a suffix character similar to `*' except that it can match the
  87.      preceding expression either once or not at all.  For example,
  88.      `ca?r' will match `car' or `cr'; nothing else.
  89.  
  90. `[ ... ]'
  91.      `[' begins a "character set", which is terminated by a `]'.  In
  92.      the simplest case, the characters between the two form the set.
  93.      Thus, `[ad]' matches either one `a' or one `d', and `[ad]*'
  94.      matches any string composed of just `a's and `d's (including the
  95.      empty string), from which it follows that `c[ad]*r' matches `cr',
  96.      `car', `cdr', `caddaar', etc.
  97.  
  98.      You can include character ranges in a character set by writing two
  99.      characters with a `-' between them.  Thus, `[a-z]' matches any
  100.      lower-case letter.  Ranges may be intermixed freely with
  101.      individual characters, as in `[a-z$%.]', which matches any lower-
  102.      case letter or `$', `%', or period.
  103.  
  104.      Note that inside a character set the usual special characters are
  105.      not special any more.  A completely different set of special
  106.      characters exists inside character sets: `]', `-', and `^'.
  107.  
  108.      To include a `]' in a character set, you must make it the first
  109.      character.  For example, `[]a]' matches `]' or `a'.  To include a
  110.      `-', write `---', which is a range containing only `-'.  To
  111.      include `^', make it other than the first character in the set.
  112.  
  113. `[^ ... ]'
  114.      `[^' begins a "complement character set", which matches any
  115.      character except the ones specified.  Thus, `[^a-z0-9A-Z]' matches
  116.      all characters except letters and digits.
  117.  
  118.      `^' is not special in a character set unless it is the first
  119.      character.  The character following the `^' is treated as if it
  120.      were first (`-' and `]' are not special there).
  121.  
  122.      Note that a complement character set can match a newline, unless
  123.      newline is mentioned as one of the characters not to match.
  124.  
  125. `^'
  126.      is a special character that matches the empty string, but only if
  127.      at the beginning of a line in the text being matched.  Otherwise,
  128.      it fails to match anything.  Thus, `^foo' matches a `foo' that
  129.      occurs at the beginning of a line.
  130.  
  131. `$'
  132.      is similar to `^' but matches only at the end of a line.  Thus,
  133.      `xx*$' matches a string of one `x' or more at the end of a line.
  134.  
  135. `\'
  136.      does two things: it quotes the special characters (including `\'),
  137.      and it introduces additional special constructs.
  138.  
  139.      Because `\' quotes special characters, `\$' is a regular
  140.      expression that matches only `$', and `\[' is a regular expression
  141.      that matches only `[', and so on.
  142.  
  143.    Note: for historical compatibility, special characters are treated as
  144. ordinary ones if they are in contexts where their special meanings make
  145. no sense.  For example, `*foo' treats `*' as ordinary since there is no
  146. preceding expression on which the `*' can act.  It is poor practice to
  147. depend on this behavior; better to quote the special character anyway,
  148. regardless of where is appears.
  149.  
  150.    Usually, `\' followed by any character matches only that character.
  151. However, there are several exceptions: characters which, when preceded
  152. by `\', are special constructs.  Such characters are always ordinary
  153. when encountered on their own.  Here is a table of `\' constructs.
  154.  
  155. `\|'
  156.      specifies an alternative.  Two regular expressions A and B with
  157.      `\|' in between form an expression that matches anything A or B
  158.      matches.
  159.  
  160.      Thus, `foo\|bar' matches either `foo' or `bar' but no other string.
  161.  
  162.      `\|' applies to the largest possible surrounding expressions.
  163.      Only a surrounding `\( ... \)' grouping can limit the grouping
  164.      power of `\|'.
  165.  
  166.      Full backtracking capability exists to handle multiple uses of
  167.      `\|'.
  168.  
  169. `\( ... \)'
  170.      is a grouping construct that serves three purposes:
  171.  
  172.        1. To enclose a set of `\|' alternatives for other operations.
  173.           Thus, `\(foo\|bar\)x' matches either `foox' or `barx'.
  174.  
  175.        2. To enclose a complicated expression for the postfix `*' to
  176.           operate on.  Thus, `ba\(na\)*' matches `bananana', etc., with
  177.           any (zero or more) number of `na' strings.
  178.  
  179.        3. To mark a matched substring for future reference.
  180.  
  181.  
  182.      This last application is not a consequence of the idea of a
  183.      parenthetical grouping; it is a separate feature which happens to
  184.      be assigned as a second meaning to the same `\( ... \)' construct
  185.      because in practice there is no conflict between the two meanings.
  186.      Here is an explanation:
  187.  
  188. `\DIGIT'
  189.      after the end of a `\( ... \)' construct, the matcher remembers the
  190.      beginning and end of the text matched by that construct.  Then,
  191.      later on in the regular expression, you can use `\' followed by
  192.      DIGIT to mean "match the same text matched the DIGIT'th time by the
  193.      `\( ... \)' construct."
  194.  
  195.      The strings matching the first nine `\( ... \)' constructs
  196.      appearing in a regular expression are assigned numbers 1 through 9
  197.      in order that the open-parentheses appear in the regular
  198.      expression.  `\1' through `\9' may be used to refer to the text
  199.      matched by the corresponding `\( ... \)' construct.
  200.  
  201.      For example, `\(.*\)\1' matches any newline-free string that is
  202.      composed of two identical halves.  The `\(.*\)' matches the first
  203.      half, which may be anything, but the `\1' that follows must match
  204.      the same exact text.
  205.  
  206.